package zhongbo.java.air.ingestion.listener;


import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.stereotype.Component;
import zhongbo.java.air.ingestion.dto.FlightPathDto;

import java.util.Date;


@Component
@Slf4j
//MessageListener Redis消息监听器
public class RedisSubscribeLogChanelListener implements MessageListener {
    @Autowired
    RedisLogChanelPublisher redisLogChanelPublisher;
    /**
     * 实际消息内容 Message message
     * 消息匹配模式 byte[] pattern
     *
     * @param message
     * @param pattern
     */
    @Override
    public void onMessage(Message message, byte[] pattern) {
        /**
         * 将消息的频道名称从字节数组转换为字符串
         */
        String channel = new String(message.getChannel());
        /**
         * 将消息体从字节数组转换为字符串
         */
        String body = new String(message.getBody());
        log.info("收到Redis日志消息 - 频道: {}, 内容: {}", channel, body);
        FlightPathDto flightPathDto = doConvert(body);
        redisLogChanelPublisher.publishMessage(flightPathDto);
    }

    private FlightPathDto doConvert(String jsonStr) {
        JSONObject jsonObject = JSONUtil.parseObj(jsonStr);
        FlightPathDto flightPathDto = new FlightPathDto();
        try {
            // 转换icao
            String icao = jsonObject.getStr("icao");
            if (icao != null) {
                    flightPathDto.setIcao_code(icao);
            }

            JSONArray jsonArray = jsonObject.getJSONArray("trace");
            if (jsonArray !=null) {
                // 转换时间戳
                Double timestamp = jsonObject.getDouble("timestamp");
                if (timestamp != null) {
                    // 获取trace[0]
                    if (jsonArray != null) {
                        Double aDouble = jsonArray.getDouble(0);
                        if (aDouble != null) {
                            flightPathDto.setTimestamp(new Date((long) ((timestamp + aDouble) * 1000)));
                        }
                    }
                }
                //转换纬度
                Double aDouble = jsonArray.getDouble(1);
                if (aDouble != null) {
                    flightPathDto.setLatitude(aDouble);
                }
                //转换经度
                Double aDouble1 = jsonArray.getDouble(2);
                if (aDouble1 != null) {
                    flightPathDto.setLongitude(aDouble1);
                }
                //转换海拔
                Double aDouble2 = jsonArray.getDouble(3);
                if (aDouble2 != null) {
                    if (aDouble2 < 0) {
                        flightPathDto.setAltitude(0.0);
                    } else {
                        flightPathDto.setAltitude(aDouble2);
                    }
                }
                Double aDouble3 = jsonArray.getDouble(4);
                if (aDouble3 != null) {
                    flightPathDto.setSpeed(aDouble3);
                }
                Double aDouble4 = jsonArray.getDouble(5);
                if (aDouble4 != null) {
                    flightPathDto.setHeading(aDouble4);
                }
                JSONObject str = jsonArray.getJSONObject(8);
                if (str != null) {
                    String res = str.getStr("type");
                    flightPathDto.setSource_type(res);
                }
            }
            log.info("整理的数据为：" + flightPathDto);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flightPathDto;
    }
}
